<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;


use Illuminate\Support\Facades\DB;
use Cache;
use Validator;
use Carbon\Carbon;

use App\Models\Weixin;
use App\Models\Phone;
use  App\Models\WeixinFriendList;
// id
// friend_username
// friend_nick
// friend_headurl
// uid
// ext
// common
// created_at
// updated_at

class FriendController extends Controller
{
    public function createList(Request $request)
    {
        $input = Input::only('data', 'userName');
        $validator = Validator::make($input, [        
            'data'      => 'required',
            'userName' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        
        $uid = $input['userName'];
        $data = json_decode($input['data'],true);
        $weixin_uid = $input['userName'];
        foreach ($data as $key => $value) {
            $weixin_data['uid'] = $value['userName'];
            $weixin_data['alias'] = $value['alias'];
            $weixin_data['nickname'] = $value['nickName'];
            $weixin_data['com_remark'] = $value['comRemark'];
            $weixin_data['sex'] = $value['sex'];
            $weixin_data['province'] = $value['provincialPy'];
            $weixin_data['city'] = $value['cityPy'];
            $weixin_data['signature'] = $value['signature'];
            $weixin_data['small_head_url'] = $value['smallHeadUrl'];
            $weixin_data['big_head_url'] = $value['bigHeadUrl'];
            $weixin_data['weixin_uid'] = $weixin_uid;
            $where['weixin_uid'] = $weixin_uid;
            $where['uid'] = $value['userName'];

            $exist = WeixinFriendList::where($where)->first();
            if(!$exist){
                WeixinFriendList::create($weixin_data);
            }
        }
        return $this->returnOk();
    }

    public function delete(Request $request)
    {
        $input = Input::only('friendUserName', 'userName');
        $validator = Validator::make($input, [        
            'friendUserName'      => 'required',
            'userName' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $where['uid'] = $input['friendUserName'];
        $where['weixin_uid'] = $input['userName'];
        $weixin = WeixinFriendList::where($where)->delete();
        if($weixin){
            return $this->returnOk();
        }else{
            return $this->returnError([],403,'数据不存在');
        }
    }

    public function create(Request $request)
    {
        $input = Input::only('fromUserName','userName','alias','nickName','comRemark','sex','provincialPy','cityPy','signature','smallHeadUrl','bigHeadUrl');
        $validator = Validator::make($input, [ 
            'fromUserName' => 'required',
            'userName' => 'nullable',
            'alias' => 'nullable',
            'nickName' => 'nullable',
            'comRemark' => 'nullable',
            'sex' => 'nullable',
            'provincialPy' => 'nullable',
            'cityPy' => 'nullable',
            'signature' => 'nullable',
            'smallHeadUrl' => 'nullable',
            'bigHeadUrl' => 'nullable',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $where['uid'] = $input['userName'];
        $where['weixin_uid'] = $input['fromUserName'];
        $exist = WeixinFriendList::where($where)->first();
        if($exist){
           
        }else{
            $data['uid'] = $input['userName'];
            $data['alias'] = $input['alias']??'';
            $data['nickname'] = $input['nickName']??'';
            $data['com_remark'] = $input['comRemark']??'';
            $data['sex'] = $input['sex'];
            $data['province'] = $input['provincialPy']??'';
            $data['city'] = $input['cityPy']??'';
            $data['signature'] = $input['signature']??'';
            $data['small_head_url'] = $input['smallHeadUrl']??'';
            $data['big_head_url'] = $input['bigHeadUrl']??'';
            $data['weixin_uid'] = $input['fromUserName']??'';
            WeixinFriendList::create($data);
        }
        return $this->returnOk();
    }
}